from system_class import System
import sys, os

def list_modules ( liwe ):
	"""
	:INPUT:
	"""
	s = System ( liwe )
	return { "modules" : s.list_modules () }

def module_list_perms ( liwe, module_name ):
	"""
	:INPUT:
		- module_name:	SAFE_DB_STRING
	"""
	s = System ( liwe )
	res = s.module_list_perms ( module_name )
	return res

def list_all_modules ( liwe ):
	"""
	:INPUT:
	"""
	s = System ( liwe )
	res = s.list_all_modules ()
	return { "modules" : res }

def module_get_all_perms ( liwe, module_names, uid = -2, perms_type = 'user' ):
	"""
	:INPUT:
		- module_names: SAFE_STRING
		- uid:		SAFE_NUMBER
		- perms_type:  	SAFE_DB_STRING
	"""
	if uid == -2:
		uid = liwe.pyhp.session.data.get ( "uid", -1 )

	s = System ( liwe )
	return { "perms" : s.module_get_all_perms ( module_names, uid, perms_type ) }

def module_get_all_group_perms ( liwe, module_name, gids = '' ):
	"""
	:INPUT:
		- module_name: 	SAFE_DB_STRING
		- gids:		SAFE_STRING
	"""
	s = System ( liwe )
	return { "perms" : s.module_get_all_group_perms ( module_name, gids ) }

def module_get_perms ( liwe, module_name, uid = -2, perms_type = 'user' ):
	"""
	:INPUT:
		- module_name:  SAFE_DB_STRING
		- uid:		SAFE_NUMBER
		- perms_type:  SAFE_DB_STRING
	"""
	if uid == -2:
		uid = liwe.pyhp.session.data.get ( "uid", -1 )

	s = System ( liwe )
	return { "perms" : s.module_get_perms ( module_name, uid, perms_type ) }

def load_page ( liwe, page_name, block_name = None ):
	"""
	:INPUT:
		- page_name:	SAFE_DB_STRING
		- block_name:	SAFE_DB_STRING
	"""
	s = System ( liwe )
	return { "html" : s.load_page ( page_name, block_name ) }

def get_templates ( liwe ):
	"""
	:INPUT:
	"""
	s = System ( liwe )
	return { "templates" : s.get_templates () }

def modules_sitemap ( liwe ):
	"""
	"""
	s = System ( liwe )

	return { "sitemaps" : s.modules_sitemap () }

def privacy ( liwe ):
	"""
		these params should be in liwe.yaml file:
		full_company_name, company_name, web_site, company_address, contact_email 
	"""
	s = System ( liwe )
	page = liwe.invoke ( "page_manager.ajax.get", id_page = 0, page_name = "privacy_def" )
	page = page.get ( 'page' )
	if not page:
		return { 'privacy': 'page not found' }
	
	from collections import defaultdict
	body = page.get ( 'txt', '' )
	cfg = liwe.cfg.get ( 'privacy', {} )
	dct = defaultdict ( unicode, cfg )
	return { "privacy" : body % dct }
